package com.gitee.sidihuo.platform.utils;

import com.gitee.sidihuo.platform.PlatformJarCompiler;
import com.gitee.sidihuo.utils.util.IpHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.concurrent.TimeUnit;

@Slf4j
@Component
public class RedisFacade {

    public static String REDIS_APP_KEY = "_platform_";

    @Autowired(required = false)
    private StringRedisTemplate stringRedisTemplate;

    @Autowired(required = false)
    private RedisTemplate<String, Object> redisTemplate;

    @Value("${app.env}")
    private String env;

    private static String IP = IpHelper.getIP();

    @PostConstruct
    private void postConstruct() {
        log.info(" startup 启动 restart 【{}】 █████████████████████████████████████████████  ENV = {}  █████████████████████████████████████████████", IP, env);
        log.info("PlatformJarCompiler version = {}", PlatformJarCompiler.compileTime);
    }

    public String buildKey(String key) {
        return env + REDIS_APP_KEY + key;
    }

    public String setCache(String key, String value, long time, TimeUnit unit) {
        return setCache(key, value, time, unit, true);
    }

    public String setCache(String key, String value, long time, TimeUnit unit, boolean logEnabled) {
        String buildKey = buildKey(key);
        ValueOperations<String, String> stringStringValueOperations = stringRedisTemplate.opsForValue();
        stringStringValueOperations.set(buildKey, value, time, unit);
        String result = stringStringValueOperations.get(buildKey);
        if (logEnabled) {
            log.info("██ setCache {} ----> {}", buildKey, value);
        }
        return result;
    }

    public String getCache(String key) {
        return getCache(key, true);
    }

    public String getCache(String key, boolean logEnabled) {
        String buildKey = buildKey(key);
        ValueOperations<String, String> stringStringValueOperations = stringRedisTemplate.opsForValue();
        String result = stringStringValueOperations.get(buildKey);
        if (logEnabled) {
            log.info("====██==== getCache {} ====> {}", buildKey, result);
        }
        return result;
    }

    public void deleteCache(String key) {
        String buildKey = buildKey(key);
        stringRedisTemplate.delete(buildKey);
    }

    public Long increment(String redisKey, int expireMinutes) {
        String buildKey = buildKey(redisKey);
        Long increment = redisTemplate.opsForValue().increment(buildKey);
        redisTemplate.expire(buildKey, expireMinutes, TimeUnit.MINUTES);
        return increment;
    }

    public Long increment(String redisKey, long delta, int expireMinutes) {
        String buildKey = buildKey(redisKey);
        Long increment = redisTemplate.opsForValue().increment(buildKey, delta);
        redisTemplate.expire(buildKey, expireMinutes, TimeUnit.MINUTES);
        return increment;
    }


}
